package com.sp.scmdb.serviceImpl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.sp.scmdb.bean.AlarmBean;
import com.sp.scmdb.bean.AlarmTypeBean;
import com.sp.scmdb.bean.EmployeeBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.AlarmDao;
import com.sp.scmdb.dao.AlarmTypeDao;
import com.sp.scmdb.dao.CallAlarmDao;
import com.sp.scmdb.entity.Alarm;
import com.sp.scmdb.entity.AlarmType; //import com.sp.scmdb.entity.Area;
import com.sp.scmdb.entity.BuMen;
import com.sp.scmdb.entity.CallAlarm;
import com.sp.scmdb.entity.Department;
import com.sp.scmdb.entity.Duty;
import com.sp.scmdb.entity.Employee; //import com.sp.scmdb.entity.Shifts;
import com.sp.scmdb.entity.WorkType;
//import com.sp.scmdb.entity.Title;
import com.sp.scmdb.entity.WorkTime; //import com.sp.scmdb.entity.WorkType;
import com.sp.scmdb.service.AlarmService;
import com.sp.scmdb.service.EmployeeService;

@Service("alarmService")
public class AlarmServiceImpl implements AlarmService {

	/** 报警Dao */
	private AlarmDao alarmDao;

	/** 报警类型Dao */
	private AlarmTypeDao alarmTypeDao;
	/** 人员类型Service */
	private EmployeeService empService;

	private CallAlarmDao callAlarmDao;

	/**
	 * @param alarmTypeDao
	 *            the alarmTypeDao to set
	 */

	@Autowired
	public void setAlarmTypeDao(AlarmTypeDao alarmTypeDao) {
		this.alarmTypeDao = alarmTypeDao;
	}
	@Autowired
	public void setCallAlarmDao(CallAlarmDao callAlarmDao) {
		this.callAlarmDao = callAlarmDao;
	}

	@Autowired
	public void setEmpService(EmployeeService empService) {
		this.empService = empService;
	}

	/**
	 * @param alarmDao
	 *            the alarmDao to set
	 */
	@Autowired
	public void setAlarmDao(AlarmDao alarmDao) {
		this.alarmDao = alarmDao;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public AlarmBean findAll(AlarmBean alarmBean, AlarmBean oldAlarmBean) {
		try {
			List<AlarmBean> listAlarm = Lists.newArrayList();
			if (alarmBean.equals(oldAlarmBean)) {
				listAlarm = oldAlarmBean.getAllList();
			} else {
				if(alarmBean.getAlarmType().equals("2") || alarmBean.getAlarmType().equals("9")){
					StringBuffer sb = new StringBuffer();
					sb.append("from CallAlarm where 1=1");

					if (StringUtils.isNotEmpty(alarmBean.getName())) {
						sb.append(" and  employee.empName like '%" + alarmBean.getName() + "%' ");
					}
					if (StringUtils.isNotEmpty(alarmBean.getAlarmType())) {
						sb.append(" and  rmk = '" + alarmBean.getAlarmType() + "' ");
					}

					if (StringUtils.isNotEmpty(alarmBean.getStartTime())) {
						sb.append(" and  alarmDate >= '" + alarmBean.getStartTime() + "' ");
					}

					if (StringUtils.isNotEmpty(alarmBean.getEndTime())) {
						sb.append(" and  alarmDate <= '" + alarmBean.getEndTime() + "' ");
					}
					if (alarmBean.getConfirmType().equals("0")) {
						sb.append(" and confirmDate is null");
					}
					if (alarmBean.getConfirmType().equals("1")) {
						sb.append(" and confirmDate is not null");
					}
					List<CallAlarm> callList = callAlarmDao.queryHql(sb.toString());
					for (CallAlarm callAlarm : callList) {
						AlarmBean newAlarm = new AlarmBean();
						if(callAlarm.getRmk().equals("2")){
							newAlarm.setAlarmType("员工报警");
						}else{
							newAlarm.setAlarmType("游客报警");
						}
						if(callAlarm.getConfirmDate()==null){
							newAlarm.setState(0);
						}else{
							newAlarm.setState(1);
						}
						if (callAlarm.getEmployee() != null) {
							if(callAlarm.getEmployee().getDepart()!=null){
								newAlarm.setDepName(callAlarm.getEmployee().getDepart().getDeptName());
							}
							newAlarm.setEmpID(String.valueOf(callAlarm.getEmployee().getId()));
							newAlarm.setEmpName(callAlarm.getEmployee().getEmpName());
						}
						newAlarm.setInTime(callAlarm.getAlarmDate());
						newAlarm.setOutTime(callAlarm.getConfirmDate());
						newAlarm.setAreaName(callAlarm.getLrmName());
						listAlarm.add(newAlarm);
					}
				}else{
					StringBuffer sb = new StringBuffer();
					sb.append("from Alarm where 1=1");
	
					if (StringUtils.isNotEmpty(alarmBean.getName())) {
						sb.append(" and  employee.empName like '%" + alarmBean.getName() + "%' ");
					}
					if (StringUtils.isNotEmpty(alarmBean.getAlarmType())) {
						sb.append(" and  alarmType = '" + alarmBean.getAlarmType() + "' ");
					}
	
					if (StringUtils.isNotEmpty(alarmBean.getStartTime())) {
						sb.append(" and  inTime >= '" + alarmBean.getStartTime() + "' ");
					}
	
					if (StringUtils.isNotEmpty(alarmBean.getEndTime())) {
						sb.append(" and  inTime <= '" + alarmBean.getEndTime() + "' ");
					}
					if (StringUtils.isNotEmpty(alarmBean.getConfirmType())) {
						sb.append(" and state = " + alarmBean.getConfirmType());
					}
					sb.append(" order by state ");
	
					List<Alarm> lists = alarmDao.queryHql(sb.toString());
					for (Alarm alarm : lists) {
						// 如果该报警是以下几种类型，则查看所关联的人员信息是否存在，
						// 如果人员信息不存在，则认为该报警已经无效了，为垃圾数据，不予统计
						if ((3 == alarm.getAlarmType() || 5 == alarm.getAlarmType()) && alarm.getEmployee() == null) {
							// 区域超时报警和园内超时报警，关联人员为空，则认为该报警信息为垃圾数据
							continue;
						}
						AlarmBean newAlarm = new AlarmBean();
						newAlarm.setAlarmType(alarm.getType().getAlarmName());
						newAlarm.setState(alarm.getState());
						if (alarm.getEmployee() != null) {
							if(alarm.getEmployee().getDepart()!=null){
								newAlarm.setDepName(alarm.getEmployee().getDepart().getDeptName());
							}
							newAlarm.setEmpID(String.valueOf(alarm.getEmployee().getId()));
							newAlarm.setEmpName(alarm.getEmployee().getEmpName());
						}
						newAlarm.setInTime(alarm.getInTime());
						newAlarm.setOutTime(alarm.getOutTime());
						if(alarm.getAlarmType()==4){
							newAlarm.setRmk("超员"+alarm.getRmk()+"人");
						}
						if (alarm.getArea() != null) {
							newAlarm.setAreaName(alarm.getArea().getName());
						}else{
							listAlarm.add(newAlarm);
							continue;
						}
	
						listAlarm.add(newAlarm);
					}
				}
			}
			if (CollectionUtils.isNotEmpty(listAlarm)) {
				alarmBean.setAllList(listAlarm);
				alarmBean.setList(listAlarm.subList(alarmBean.getCurrentRow(), alarmBean.getLastRow()));
			}
			return alarmBean;
		} catch (Exception e) {
			throw new IllegalStateException(e.getMessage());
		}
	}

	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<AlarmType> findAllAlarmType() {
		String hql = "from AlarmType where isUse=1";
		return alarmTypeDao.queryHql(hql);
	}

	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public List<AlarmType> findAlarmType() {
		String hql = "from AlarmType";
		return alarmTypeDao.queryHql(hql);
	}

	public AlarmBean findEmp(AlarmBean entity, String empId, UserBean user) {

		// 先获取集合数据
		List<Department> departments = empService.getListDepartment(user);
		List<Duty> dutys = empService.getListDuty();
		List<WorkType> worktypes = empService.getListWorkType();
		List<BuMen> bumens =empService.getListBuMen();
		// 防止空指针 初始化
		EmployeeBean bean = new EmployeeBean();
		entity.setEmployeeBean(bean);

		// 根据ID查找具体的人员信息
		Employee emp = empService.getById(empId);

		/** 开始封装数据到Bean */

		entity.getEmployeeBean().setEmpDepID(emp.getEmpDepID());
		// entity.getEmployeeBean().setEmpDutyID(emp.getEmpDutyID());
		entity.getEmployeeBean().setEmpWorkTime(emp.getEmpWorkTime());
		entity.getEmployeeBean().setEmpMaxWorkTime(emp.getEmpMaxWorkTime());
	    entity.getEmployeeBean().setEmpWorkID(emp.getEmpWorkID());
		entity.getEmployeeBean().setId(empId);
		entity.getEmployeeBean().setEmpLabelID(emp.getEmpLableID());
		entity.getEmployeeBean().setEmpName(emp.getEmpName());
		entity.getEmployeeBean().setEmpAddr(emp.getEmpAddr());
		entity.getEmployeeBean().setEmpPhone(emp.getEmpPhone());
		entity.getEmployeeBean().setProName(emp.getProName());
		entity.getEmployeeBean().setEmpBuMenID(emp.getEmpBuMenID());
		if (emp.getEmpPhoto() != null) {
			entity.getEmployeeBean().setEmpPhotoFileName("");
		}
		entity.getEmployeeBean().setPhoto(emp.getEmpPhoto());
		if(emp.getEmpSex().endsWith("1")){
			entity.getEmployeeBean().setEmpSex("男");
		}else{
			entity.getEmployeeBean().setEmpSex("女");
		}
		
		entity.getEmployeeBean().setEmpMaxWorkTime(emp.getEmpMaxWorkTime());

		// 封装班组
		for (int i = 0; i < departments.size(); i++) {
			Department ds = departments.get(i);
			if (ds.getId().toString().equals(emp.getEmpDepID())) {
				entity.getEmployeeBean().setEmpDepID(ds.getDeptName());
				break;
			}
			else{
				entity.getEmployeeBean().setEmpDepID("无");
			}

		}
		// 封装部门
		for (int i = 0; i < bumens.size(); i++) {
			BuMen ds = bumens.get(i);
			if (ds.getId().toString().equals(emp.getEmpBuMenID())) {
				entity.getEmployeeBean().setEmpBuMenID(ds.getName());
				break;
			}
			else{
				entity.getEmployeeBean().setEmpBuMenID("无");
			}

		}

		// 封装工种
		for (int i = 0; i < worktypes.size(); i++) {
			WorkType ds = worktypes.get(i);
			if (ds.getId().toString().equals(emp.getEmpWorkTypeID())) {
				entity.getEmployeeBean().setEmpWorkTypeID(ds.getName());
				break;
			}else
			{
				entity.getEmployeeBean().setEmpWorkTypeID("无");
			}

		}
/*		// 封装职务
		for (int j = 0; j < dutys.size(); j++) {
			Duty dt = dutys.get(j);
			if (dt.getId().toString().equals(emp.getEmpDutyID())) {
				entity.getEmployeeBean().setEmpDutyID(dt.getName());
			}
			entity.getEmployeeBean().setEmpDutyID("无");
		}*/


		return entity;
	}

	@Transactional(readOnly = true, isolation = Isolation.READ_UNCOMMITTED)
	public AlarmTypeBean findAlarmTypeByID(String id) {
		AlarmTypeBean alarmTypeBean = null;
		AlarmType alarmType = alarmTypeDao.getById(Integer.parseInt(id));
		if (alarmType != null) {
			alarmTypeBean = new AlarmTypeBean();
			alarmTypeBean.setId(alarmType.getId());
			alarmTypeBean.setAlarmName(alarmType.getAlarmName());
			alarmTypeBean.setEdEsc(alarmType.getEdEsc());
			alarmTypeBean.setIsUse(alarmType.getIsUse());
		}
		return alarmTypeBean;
	}

	@Transactional
	public String editAlarmType(AlarmTypeBean entity) {
		AlarmType alarmType = new AlarmType();
		String result = CommonParamter.CHECK_FAIL;
		if (entity.getId() != null) {
			alarmType.setId(entity.getId());
			alarmType.setAlarmName(entity.getAlarmName());
			alarmType.setIsUse(entity.getIsUse());
			alarmTypeDao.update(alarmType);
			result = CommonParamter.CHECK_SUCCESS;
		}
		return result;
	}
}
